package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleService;
import com.heima.article.service.IPublishService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-11-11
 */

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    private ApArticleContentServiceImpl apArticleContentService;
    @Autowired
    @Lazy
    private IPublishService iPublishService;
    /**
     * 保存文章和更新文章
     *
     * @param dto
     * @return
     */
    @Override
    @Async
    public ResponseResult<Long> saveArticle(ArticleDto dto) {
        //判断是否存在id,存在则更新,不存在则新增
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        if(dto.getId()==null){
            this.save(apArticle);
            //文章内容
            ApArticleContent articleContent = new ApArticleContent();
            articleContent.setArticleId(apArticle.getId());
            articleContent.setContent(dto.getContent());
            //保存
            apArticleContentService.save(articleContent);
            //自动生成静态页面
            iPublishService.createHtml(apArticle, dto.getContent());
            return ResponseResult.okResult(apArticle.getId());
        }
        //存在则更新
        this.updateById(apArticle);
        LambdaUpdateWrapper<ApArticleContent> update=new LambdaUpdateWrapper<>();
        update.eq(ApArticleContent::getArticleId,apArticle.getId());
        update.set(ApArticleContent::getContent,dto.getContent());
        apArticleContentService.update(update);

        return ResponseResult.okResult(apArticle.getId());
    }

    /**
     * 查看文章数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        //根据id查看文章
        ApArticle apArticle = this.getById(dto.getArticleId());
        //查看文章内容
        LambdaQueryWrapper<ApArticleContent> query=new LambdaQueryWrapper<>();
        query.eq(ApArticleContent::getArticleId,dto.getArticleId());
        ApArticleContent content = apArticleContentService.getOne(query);
        //定义map集合
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("config",apArticle);
        resultMap.put("content",content);
        return ResponseResult.okResult(resultMap);
    }

    /**
     * 首页文章数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto,int type) {
        //首页
        IPage<ApArticle> page=new Page<>(1,dto.getSize());
        LambdaQueryWrapper<ApArticle> query=new LambdaQueryWrapper<>();
        //排序倒叙
        query.orderByDesc(ApArticle::getPublishTime);
        // 如果频道ID不为0 ,添加频道id查询
        if(dto.getChannelId()!=0){
            query.eq(ApArticle::getChannelId,dto.getChannelId());
        }
        // 文章的状态不能是已下架和已删除
        query.eq(ApArticle::getIsDelete, false);
        query.eq(ApArticle::getIsDown, false);
        //加载更多数据--> 小于当前时间
        if(type==1){
            query.lt(ApArticle::getPublishTime,dto.getMinTime());
        }else {
            //加载最新数据--> 大于当前时间
            query.gt(ApArticle::getPublishTime,dto.getMaxTime());
        }
        IPage<ApArticle> iPage = this.page(page, query);
        return ResponseResult.okResult(iPage.getRecords());
    }
}
